package com.wetsoft.base

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WorkCount {

  def main(args: Array[String]): Unit = {


    val conf = new SparkConf()
    conf.setMaster("local").setAppName("WC");
    val sc = new SparkContext(conf)

    val lines : RDD[String] = sc.textFile("E:/wordCount.txt")
    val word : RDD[String] = lines.flatMap(lines => {
        lines.split(" ")
    })

    val pairs : RDD[(String,Int)] = word.map{x => (x,1)}
    val result = pairs.reduceByKey{(a,b)=> {a+b}}
    result.sortBy(_._2,false).foreach(println)


    //简化写法
//    lines.flatMap { _.split(" ")}.map { (_,1)}.reduceByKey(_+_).foreach(println)
  }

}
